package com.pro.controller;

import com.pro.pojo.JsCarousel;
import com.pro.pojo.JsonData;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.pro.service.JsCarouselService;
import com.pro.utils.FastDFSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author 祖铭泽
 * @Date 2021/11/15/0015
 * @description
 */
@Controller
@RequestMapping(value = "/JsCarousel")
public class JsCarouselController {
    @Autowired
    private JsCarouselService jsCarouselService;
    @Autowired
    private FastDFSClientUtil fsc;
    //添加轮播图时 上传图片
    @RequestMapping(value = "/uploadImagesCarousel")
    @ResponseBody
    public JsonData uploadImagesCarousel(MultipartFile file, HttpServletRequest request) throws IOException {
        String fileName = null;
        String nameSuffix = null;
        String resAccessUrl = null;
        if(file.getSize() > 0) {
            fileName = file.getOriginalFilename();
            nameSuffix = fileName.substring(fileName.indexOf("."));
            fileName = "slideshow" + new Date().getTime() + nameSuffix;
            //数据库文件路径
            String fileUrl = fsc.uploadFile(file);
            //前台传递路径
            resAccessUrl = fsc.getResAccessUrl(fileUrl);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 0);
        HashMap<String, Object> data = new HashMap<>();
        data.put("src", resAccessUrl);
        data.put("fileName", fileName);
        data.put("imgType", nameSuffix);
        map.put("data", data);
        return map.get("data") != null ? JsonData.buildSuccess(map) : JsonData.buildError("上传失败");
    }
    //添加轮播图
    @RequestMapping(value = "/addJsCarousel")
    @ResponseBody
    public JsonData addJsCarousel(JsCarousel jsCarousel){
        boolean addJsCarousel = jsCarouselService.addJsCarousel(jsCarousel);
        return addJsCarousel ? JsonData.buildSuccess(jsCarousel) : JsonData.buildError("添加失败");
    }

    //根据图片类型和显示状态查询轮播图
    @RequestMapping(value = "/getSlideshowsByDisplayImgType")
    @ResponseBody
    public JsonData getSlideshowsByDisplayImgType(JsCarousel jsCarousel){
        List<JsCarousel> slideshowsByDisplayImgType = jsCarouselService.getSlideshowsByDisplayImgType(jsCarousel);
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg","");
        map.put("data",slideshowsByDisplayImgType);
        map.put("code",0);
        return map.get("data") != null ? JsonData.buildSuccess(slideshowsByDisplayImgType) : JsonData.buildError("服务器出错了");
    }

    //删除当前轮播图
    @RequestMapping(value = "/delCurrentSlideshow")
    @ResponseBody
    public JsonData delCurrentSlideshow(Integer carouselId){
        boolean delCurrentSlideshow = jsCarouselService.delCurrentSlideshow(carouselId);
        return delCurrentSlideshow ? JsonData.buildSuccess("删除成功") : JsonData.buildError("删除失败");
    }

    //更改当前轮播图
    @RequestMapping(value = "/updateJsCarousel")
    @ResponseBody
    public JsonData updateJsCarousel(JsCarousel jsCarousel){
        String displayName = jsCarousel.getDisplayName();
        if("上线".equals(displayName)){
            jsCarousel.setDisplay(2);
        }else if("下线".equals(displayName)){
            jsCarousel.setDisplay(3);
        }
        boolean delCurrentSlideshow = jsCarouselService.updateJsCarousel(jsCarousel);
        return delCurrentSlideshow ? JsonData.buildSuccess("更改成功") : JsonData.buildError("更改失败");
    }

    //查询当前用户广告即将到期条数
    @RequestMapping(value = "/getAdvertisingCountsByCreateId")
    @ResponseBody
    public JsonData getAdvertisingCountsByCreateId(Integer createId){
        //获取当前时间戳
        long currentTime = new Date().getTime();
        Integer count = 0;
        //从数据库查出当前用户的所有广告到期时间
        List<JsCarousel> advertisingCountsByCreateId = jsCarouselService.getAdvertisingCountsByCreateId(createId);
        for (int i = 0; i < advertisingCountsByCreateId.size(); i++) {
            //获取当前每个广告到期时间
            long expireDate = advertisingCountsByCreateId.get(i).getExpireDate().getTime();
            //如果 广告到期时间小于等于3天 则计数一次
            if(expireDate - currentTime < 0){
                continue;
            }else if(expireDate - currentTime <= 259200000){
                count++;
            }
        }
        return count != null  ? JsonData.buildSuccess(count) : JsonData.buildError("更改失败");
    }
}
